package com.ncloud.system.mapper;

import com.ncloud.system.entity.DepartRole;
import com.ncloud.system.entity.Menu;
import com.ncloud.system.entity.Role;
import com.ncloud.system.entity.RoleMenu;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by shiqiang on 2017/7/28.
 */
@Mapper
public interface RoleMapper {
    @Select(value = { "select a.*,b.company_name from sys_role a,sys_company b where a.company_id=b.company_id  and a.use_tag='1' and a.role_id=#{0}" })
    @Results({ @Result(column = "role_id", property = "roleId"),
            @Result(column = "company_id", property = "companyId"),
            @Result(column = "company_name", property = "companyName"),
            @Result(column = "role_name", property = "roleName"), @Result(column = "use_tag", property = "useTag"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    public Role getOneRole(Long roleId);

    @Insert("insert into sys_role(role_id,company_id, role_name, use_tag,create_user, create_date, update_date, update_user, remarks)"
            + " values(#{roleId},#{companyId}, #{roleName}, #{useTag},#{createUser}, #{createDate}, #{updateDate}, #{updateUser}, #{remarks})")
    public int insertRole(Role role);

    @Update("update sys_role set company_id=#{companyId}, role_name=#{roleName}, use_tag=#{useTag}, "
            + " update_date=#{updateDate}, update_user=#{updateUser} where role_id=#{roleId} and use_tag='1'")
    public int updateRole(Role role);

    /*根据公司查询其下的所有角色，没有有效tag*/
    @Select(value = { "select a.*,b.company_name from sys_role a,sys_company b where a.company_id=b.company_id  and a.use_tag='1' and a.company_id=#{0}" })
    @Results({ @Result(column = "role_id", property = "roleId"),
            @Result(column = "company_id", property = "companyId"),
            @Result(column = "company_name", property = "companyName"),
            @Result(column = "role_name", property = "roleName"), @Result(column = "use_tag", property = "useTag"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    public List<Role> getRolesByCompanyId(Long companyId);

    /*根据部门和所在的公司查询，所有的部门角色关系*/
    @Select(value = { "SELECT DISTINCT a.*  FROM sys_depart_role a ,sys_role b where a.ROLE_ID=b.ROLE_ID and b.use_tag='1'  and a.DEPART_ID=#{0} and b.company_id =#{1}" })
    @Results({ @Result(column = "depart_id", property = "departId"),
            @Result(column = "role_id", property = "roleId"),
            @Result(column = "use_tag", property = "useTag")})
    public List<DepartRole> getDepartRoleByDepart(Long departId, Long companyId);


    /*根据部门和所在的公司查询，所有的部门角色关系*/
    @Select(value = { "SELECT DISTINCT b.*  FROM sys_depart_role a ,sys_role b where a.ROLE_ID=b.ROLE_ID and b.use_tag='1'  and a.DEPART_ID=#{0} and b.company_id =#{1}" })
    @Results({ @Result(column = "role_id", property = "roleId"), @Result(column = "company_id", property = "companyId"),
            @Result(column = "role_name", property = "roleName"), @Result(column = "use_tag", property = "useTag"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    public List<Role> getRoleByDepart(Long departId, Long companyId);


    /*根据公司的角色，查询对应的菜单*/
    @Select(value = { "select DISTINCT b.* from sys_role_menu a ,sys_menu b,sys_role c  where a.MENU_ID =b.MENU_ID " +
            " and c.ROLE_ID = a.ROLE_ID and b.use_tag='1' and c.use_tag='1' and a.ROLE_ID =#{0} and c.company_id =#{1}"
    })
    @Results({ @Result(column = "menu_id", property = "menuId"), @Result(column = "menu_name", property = "menuName"),
            @Result(column = "parent_id", property = "parentId"), @Result(column = "menu_url", property = "menuUrl"),
            @Result(column = "menu_desc", property = "menuDesc"), @Result(column = "icon", property = "icon"),
            @Result(column = "use_tag", property = "useTag"), @Result(column = "sort", property = "sort"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    public List<Menu> getMenusByCompanyRole(Long roleId, Long companyId);

    /*根据角色，查询对应的菜单*/
    @Select(value = { "select b.* from sys_role_menu a ,sys_menu b where a.MENU_ID =b.MENU_ID and b.use_tag='1' and a.ROLE_ID =#{0}"
    })
    @Results({ @Result(column = "menu_id", property = "menuId"), @Result(column = "menu_name", property = "menuName"),
            @Result(column = "parent_id", property = "parentId"), @Result(column = "menu_url", property = "menuUrl"),
            @Result(column = "menu_desc", property = "menuDesc"), @Result(column = "icon", property = "icon"),
            @Result(column = "use_tag", property = "useTag"), @Result(column = "sort", property = "sort"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    public List<Menu> getMenusByRole(Long roleId);

    /*删除角色*/
    @Delete("delete from sys_role  where role_id=#{0} and use_tag='1'")
    public int deleteRole(Long roleId);


    @Update("update sys_role set use_tag='0' where role_id=#{0} and use_tag='1'")
    public int deleteRoleState(Long roleId);
    /*插入角色菜单关系*/

    @Insert("insert into sys_role_menu(role_id,menu_id, use_tag)"
            + " values(#{roleId},#{menuId}, #{useTag})")
    public int insertRoleMenu(RoleMenu roleMenu);

    /*删除角色菜单关系*/
    @Delete("delete from sys_role_menu where role_id=#{0} and use_tag='1'")
    int deleteRoleMenu(Long roleId);

    @Update("update sys_role_menu set use_tag='0' where role_id=#{0} and use_tag='1'")
    public int deleteRoleMenuState(Long roleId);

    /*根据角色*/
    @Select(value = { "select a.*,b.company_name from sys_role a,sys_company b where a.company_id=b.company_id  and a.use_tag='1'" })
    @Results({ @Result(column = "role_id", property = "roleId"),
            @Result(column = "company_id", property = "companyId"),
            @Result(column = "company_name", property = "companyName"),
            @Result(column = "role_name", property = "roleName"), @Result(column = "use_tag", property = "useTag"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    public List<Role> getRolesByAll();
}
